<template>
  <div class="gomoku-container">
    <div class="game-header">
      <h1>五子棋</h1>
      <div class="game-info">
        <div class="player-info">
          <div class="player" :class="{ active: currentPlayer === 'black' }">
            <div class="stone black"></div>
            <span>黑方{{ currentPlayer === 'black' ? '（轮到你）' : '' }}</span>
          </div>
          <div class="player" :class="{ active: currentPlayer === 'white' }">
            <div class="stone white"></div>
            <span>白方{{ currentPlayer === 'white' ? '（轮到你）' : '' }}</span>
          </div>
        </div>
        <div class="game-controls">
          <button @click="resetGame" class="control-btn">重新开始</button>
          <button @click="undoMove" class="control-btn" :disabled="moveHistory.length === 0">撤销</button>
        </div>
      </div>
    </div>

    <div class="game-status" v-if="gameStatus">
      {{ gameStatus }}
    </div>

    <div class="board-container">
      <div class="board" ref="boardRef">
        <div 
          v-for="(row, rowIndex) in board" 
          :key="'row-' + rowIndex" 
          class="board-row"
        >
          <div 
            v-for="(cell, colIndex) in row" 
            :key="'cell-' + rowIndex + '-' + colIndex" 
            class="board-cell"
            @click="placeStone(rowIndex, colIndex)"
          >
            <div 
              v-if="cell" 
              class="stone" 
              :class="cell"
              :data-last="isLastMove(rowIndex, colIndex) ? 'true' : 'false'"
            ></div>
            <div class="board-dot" v-if="isDotPosition(rowIndex, colIndex)"></div>
          </div>
        </div>
      </div>
    </div>

    <div class="game-rules">
      <h3>游戏规则</h3>
      <p>1. 黑方先行，双方轮流在棋盘交叉点上放置棋子</p>
      <p>2. 先在横、竖或斜方向上形成连续五子的一方获胜</p>
      <p>3. 黑方禁止使用"三三禁手"和"四四禁手"</p>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';

// 棋盘大小（15x15）
const BOARD_SIZE = 15;

// 初始化棋盘
const initBoard = () => {
  return Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
};

const board = ref(initBoard());
const currentPlayer = ref('black'); // 'black' 或 'white'
const gameOver = ref(false);
const winner = ref(null);
const lastMove = ref(null);
const moveHistory = ref([]);
const boardRef = ref(null);

// 游戏状态信息
const gameStatus = computed(() => {
  if (gameOver.value) {
    if (winner.value) {
      return `游戏结束！${winner.value === 'black' ? '黑方' : '白方'}获胜！`;
    } else {
      return '游戏结束！平局！';
    }
  }
  return '';
});

// 判断是否是标准点位置（用于显示小黑点）
const isDotPosition = (row, col) => {
  // 中心点
  if (row === 7 && col === 7) return true;
  
  // 星位
  if ((row === 3 || row === 11) && (col === 3 || col === 11)) return true;
  
  return false;
};

// 判断是否是最后一步
const isLastMove = (row, col) => {
  return lastMove.value && lastMove.value.row === row && lastMove.value.col === col;
};

// 放置棋子
const placeStone = (row, col) => {
  // 如果游戏已结束或该位置已有棋子，则不允许放置
  if (gameOver.value || board.value[row][col] !== null) {
    return;
  }

  // 放置棋子
  board.value[row][col] = currentPlayer.value;
  
  // 记录最后一步
  lastMove.value = { row, col, player: currentPlayer.value };
  
  // 添加到历史记录
  moveHistory.value.push({ row, col, player: currentPlayer.value });

  // 检查是否获胜
  if (checkWin(row, col, currentPlayer.value)) {
    gameOver.value = true;
    winner.value = currentPlayer.value;
    return;
  }

  // 切换玩家
  currentPlayer.value = currentPlayer.value === 'black' ? 'white' : 'black';
};

// 检查是否获胜
const checkWin = (row, col, player) => {
  const directions = [
    [0, 1],   // 水平
    [1, 0],   // 垂直
    [1, 1],   // 对角线 /
    [1, -1]   // 对角线 \
  ];

  for (const [dx, dy] of directions) {
    let count = 1;  // 当前位置已经有一个棋子

    // 向一个方向检查
    for (let i = 1; i <= 4; i++) {
      const newRow = row + i * dx;
      const newCol = col + i * dy;
      
      if (
        newRow >= 0 && newRow < BOARD_SIZE &&
        newCol >= 0 && newCol < BOARD_SIZE &&
        board.value[newRow][newCol] === player
      ) {
        count++;
      } else {
        break;
      }
    }

    // 向相反方向检查
    for (let i = 1; i <= 4; i++) {
      const newRow = row - i * dx;
      const newCol = col - i * dy;
      
      if (
        newRow >= 0 && newRow < BOARD_SIZE &&
        newCol >= 0 && newCol < BOARD_SIZE &&
        board.value[newRow][newCol] === player
      ) {
        count++;
      } else {
        break;
      }
    }

    if (count >= 5) {
      return true;
    }
  }

  return false;
};

// 撤销上一步
const undoMove = () => {
  if (moveHistory.value.length === 0 || gameOver.value) {
    return;
  }

  const lastMove = moveHistory.value.pop();
  board.value[lastMove.row][lastMove.col] = null;
  
  // 更新最后一步
  if (moveHistory.value.length > 0) {
    const prevMove = moveHistory.value[moveHistory.value.length - 1];
    lastMove.value = { row: prevMove.row, col: prevMove.col, player: prevMove.player };
  } else {
    lastMove.value = null;
  }

  // 切换回上一个玩家
  currentPlayer.value = lastMove.player;
  
  // 重置游戏状态
  gameOver.value = false;
  winner.value = null;
};

// 重置游戏
const resetGame = () => {
  board.value = initBoard();
  currentPlayer.value = 'black';
  gameOver.value = false;
  winner.value = null;
  lastMove.value = null;
  moveHistory.value = [];
};

onMounted(() => {
  // 可以在这里添加初始化逻辑
});
</script>

<style scoped>
.gomoku-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.game-header {
  margin-bottom: 20px;
}

.game-header h1 {
  font-size: 24px;
  margin-bottom: 16px;
  color: var(--theme-text-color, #1e293b);
}

.game-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.player-info {
  display: flex;
  gap: 20px;
}

.player {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background-color 0.2s;
}

.player.active {
  background-color: var(--theme-hover-bg, #f1f5f9);
}

.stone {
  width: 20px;
  height: 20px;
  border-radius: 50%;
}

.stone.black {
  background: radial-gradient(circle at 30% 30%, #666, #000);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
}

.stone.white {
  background: radial-gradient(circle at 30% 30%, #fff, #ddd);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  border: 1px solid #ccc;
}

.game-controls {
  display: flex;
  gap: 10px;
}

.control-btn {
  padding: 8px 16px;
  background-color: var(--theme-bg, #ffffff);
  border: 1px solid var(--theme-border-color, #e2e8f0);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.control-btn:hover:not(:disabled) {
  background-color: var(--theme-hover-bg, #f1f5f9);
}

.control-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.game-status {
  padding: 10px;
  margin-bottom: 20px;
  background-color: var(--theme-hover-bg, #f1f5f9);
  border-radius: 8px;
  text-align: center;
  font-weight: bold;
  color: var(--theme-text-color, #1e293b);
}

.board-container {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
}

.board {
  display: inline-block;
  background-color: #e6c88e;
  padding: 10px;
  border-radius: 4px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.board-row {
  display: flex;
}

.board-cell {
  position: relative;
  width: 30px;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.board-cell::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 1px;
  background-color: #000;
}

.board-cell::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background-color: #000;
}

.board-row:first-child .board-cell::before {
  top: 50%;
}

.board-row:last-child .board-cell::before {
  bottom: 50%;
}

.board-row .board-cell:first-child::after {
  left: 50%;
}

.board-row .board-cell:last-child::after {
  right: 50%;
}

.board-dot {
  position: absolute;
  width: 6px;
  height: 6px;
  background-color: #000;
  border-radius: 50%;
  z-index: 1;
}

.stone {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  z-index: 2;
  transition: transform 0.2s;
}

.stone[data-last="true"] {
  box-shadow: 0 0 0 2px rgba(255, 0, 0, 0.5);
}

.game-rules {
  background-color: var(--theme-bg, #ffffff);
  border: 1px solid var(--theme-border-color, #e2e8f0);
  border-radius: 8px;
  padding: 16px;
}

.game-rules h3 {
  margin-top: 0;
  margin-bottom: 12px;
  font-size: 18px;
  color: var(--theme-text-color, #1e293b);
}

.game-rules p {
  margin: 8px 0;
  color: var(--theme-text-color, #64748b);
}

/* 暗色主题 */
@media (prefers-color-scheme: dark) {
  .board {
    background-color: #b89b62;
  }
  
  .stone.white {
    background: radial-gradient(circle at 30% 30%, #fff, #ccc);
    border: 1px solid #999;
  }
  
  .game-rules {
    background-color: var(--theme-bg, #1e293b);
    border-color: var(--theme-border-color, #334155);
  }
  
  .game-rules h3 {
    color: var(--theme-text-color, #e2e8f0);
  }
  
  .game-rules p {
    color: var(--theme-text-color, #94a3b8);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .gomoku-container {
    padding: 10px;
  }
  
  .game-info {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  
  .board-cell {
    width: 24px;
    height: 24px;
  }
  
  .stone {
    width: 20px;
    height: 20px;
  }
}

@media (max-width: 480px) {
  .board-cell {
    width: 20px;
    height: 20px;
  }
  
  .stone {
    width: 16px;
    height: 16px;
  }
  
  .board-dot {
    width: 4px;
    height: 4px;
  }
}
</style> 